SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[SP_TDORDE_FACT_Q01]  
/*-------------------------------------------------------*/  
/*---Empresa              : OFISIS S.A.                --*/  
/*---Cliente              : OFISIS                --*/  
/*---Sistema              : Tesoreria                  --*/  
/*---M«dulo               : Empresa                    --*/  
/*---Programa             : Consulta saldos de partidas--*/  
/*---Script               : SP_TDORDE_FACT_Q01.sql     --*/  
/*---Nombre SP            : SP_TDORDE_FACT_Q01         --*/  
/*---Desarrollado por     : Denis Villacr's        --*/  
/*---Fecha Creaci«n       : 26/11/2003                 --*/  
/*---Base Datos           : Microsoft Sql Server       --*/  
/*---Versi«n              : 2000                       --*/  
/*---Invoca a SP          :                            --*/  
/*-------------------------------------------------------*/  
  
/*-------------------------------------------------------*/  
/*---Modificado 2 por     :           --*/  
/*---Fecha Modificaci«n   :           --*/   
/*---Detalle Modificaci«n :           --*/  
/*-------------------------------------------------------*/  
  
/*-------------------------------------------------------*/  
/*---Comentarios                                  --*/  
/*---                                                  --*/  
/*---                                                  --*/  
/*-------------------------------------------------------*/  
  
/*-------------------------------------------------------*/  
/* DROP PROC SP_TDORDE_FACT_Q01     */  
/* GRANT ALL ON SP_TDORDE_FACT_Q01 TO PUBLIC     */  
/* SP_TDORDE_FACT_Q01                                    */  
/*-------------------------------------------------------*/  
@ISCO_EMPR       TD_VC_003,  
@ISCO_USUA_MODI  TD_VC_008,  
@ISCO_PROV       TD_VC_020,  
@ISCO_TIPO_DOCU  TD_VC_003,  
@ISNU_DOCU_PROV  TD_VC_020,   
@ISNU_DOCU_ORIG  TD_VC_020, -- Nro. Orden de Compra  
@ISCO_MONE       TD_VC_003,  
@ISNU_DOCU_GUIN  TD_VC_020,  
@INFA_TIPO_CAMB  TD_NU_009_004
	As
Begin  
  
Declare  
  @VNNU_CORR       TD_IN_001,  
  @VSST_CNTR_PRES  TD_VC_001,   
  @VNAA_CNTR       TD_IN_001,   
  @VNMM_CNTR       TD_IN_001,   
  @VNNU_FETC_0001  TD_IN_001, /* Status del cursor */  
  @CSCO_PART    TD_VC_020,   
  @VSTI_SITU    TD_VC_003,   
  @VSST_CNTR_PARA  TD_VC_001,   
  @VNIM_PART     TD_NU_016_004,   
  @VNCA_PART       TD_IN_001,  
  @VNNU_SECU       TD_IN_001,  
  
  @OSVA_MENS       TD_VC_100,   
  @VNIM_SALD    TD_NU_016_004,   
  @VSST_SIGN    TD_VC_001,   
  @VNIM_REAL    TD_NU_016_004,   
  @VSCO_MONE_PART  TD_VC_003,   
  @VNIM_UNIT    TD_NU_016_004,   
  @VSCO_MONE_ORCO  TD_VC_003,   
  @VDFE_EMIS_ORCO  TD_DT_001,   
  @VDFE_EMIS_DOCU  TD_DT_001,   
  @VSTI_CAMB_PROV  TD_VC_001,   
  @VSTI_ORIG    TD_VC_001,   
  @VSTI_CAMB    TD_VC_001,  
  @VSCO_MONE_NACI  TD_VC_003,   
  @VSCO_MONE_EXTR  TD_VC_003  
  
--Begin Tran SP_TDORDE_FACT_Q01  

  
--------------- CONTROL PRESUPUESTAL (INICIO) ---------------  
  
/*Para control de presupuestos*/  

/* 
-- se habilitara con presupuestos
 Select  @VNAA_CNTR = Year(FE_EMIS),   
  @VNMM_CNTR = Month(FE_EMIS),   
  @VDFE_EMIS_DOCU = FE_EMIS  
 From TMDOCU_PROV  
 Where CO_EMPR = @ISCO_EMPR  
  And CO_PROV = @ISCO_PROV  
  And CO_TIPO_DOCU = @ISCO_TIPO_DOCU  
  And NU_DOCU_PROV = @ISNU_DOCU_PROV  
  
 Select @VSST_CNTR_PRES = ST_CNTR_PRES  
 From TMPARA_PRES  
 Where CO_EMPR = @ISCO_EMPR  
  And NU_ANNO = @VNAA_CNTR  
  
        Select @VSCO_MONE_ORCO = CO_MONE,   
        @VDFE_EMIS_ORCO = FE_EMIS  
        From   TCORDE_COMP   
 Where  CO_EMPR = @ISCO_EMPR   
 And    NU_ORCO = @ISNU_DOCU_ORIG   
  
        Select  @VSTI_CAMB = TI_CAMB,   
         @VSTI_ORIG = TI_ORIG,   
         @VSCO_MONE_NACI = CO_MONE_DEFA,   
  @VSCO_MONE_EXTR = CO_MONE_EXTR  
        From    TMPARA_TESO  
        Where   CO_EMPR = @ISCO_EMPR   
  
If @VSST_CNTR_PRES = 'S'  
  Begin 
  
Declare CU_TDORDE_FACT_I01_1 Cursor   
For  
Select  Distinct(CO_PART)  
From   TDORDE_COMP  
Where  NU_ORCO = @ISNU_DOCU_ORIG  
 And CO_EMPR = @ISCO_EMPR   
 And Rtrim(Ltrim(CO_PART)) != ''  
  
 Select   @VNNU_SECU = 0  --Abre Cursor de Interfase - Cabecera   
Open CU_TDORDE_FACT_I01_1  
Fetch  CU_TDORDE_FACT_I01_1 Into @CSCO_PART  
  
  
Select @VNNU_FETC_0001 = @@FETCH_STATUS  
  
While (@VNNU_FETC_0001 = 0)  
  Begin  
    If (@VNNU_FETC_0001 < 0)  
        Begin  
            RAISERROR 20001 'FIN DE ARCHIVO'   
     CLOSE CU_TDORDE_FACT_I01_1   
     DEALLOCATE CU_TDORDE_FACT_I01_1   
     RETURN   
        End          
  
  
 If Not Exists( Select CO_EMPR  
         From   TRSIST_PART  
         Where  CO_EMPR = @ISCO_EMPR  
         And NU_ANNO = @VNAA_CNTR  
         And CO_SIST = 'OFITESO'  
         And CO_PART = @CSCO_PART )  
 Begin  
    Select @VNNU_SECU = @VNNU_SECU + 1   
    Insert Into #TWTDORDE_FACT_Q01  
  (NU_SECU, CO_PART_OUTP, NU_GUIN_OUTP, NU_ORCO_OUTP, ST_REGI_PART)  
    Values ( @VNNU_SECU, @CSCO_PART, @ISNU_DOCU_GUIN, @ISNU_DOCU_ORIG, 'N' )  
 End  
  
  
 If Exists( Select CO_EMPR  
     From   TRSIST_PART  
     Where  CO_EMPR = @ISCO_EMPR  
     And NU_ANNO = @VNAA_CNTR  
     And CO_SIST = 'OFITESO'  
     And CO_PART = @CSCO_PART )  
   
 Begin  
  
   Select @VSTI_SITU = TI_SITU,   
   @VSST_CNTR_PARA = ST_CNTR_PRES,   
   @VSCO_MONE_PART = CO_MONE  
   From  TMPART  
   Where  CO_EMPR = @ISCO_EMPR   
   And NU_ANNO = @VNAA_CNTR   
   And CO_PART = @CSCO_PART   
  
   If @VSTI_SITU != 'ACT'  
   Begin  
     Select @VNNU_SECU = @VNNU_SECU + 1   
     Insert Into #TWTDORDE_FACT_Q01  
   (NU_SECU, CO_PART_OUTP, NU_GUIN_OUTP, NU_ORCO_OUTP, ST_ACTV_PART)  
    Values ( @VNNU_SECU, @CSCO_PART, @ISNU_DOCU_GUIN, @ISNU_DOCU_ORIG, 'N' )  
  End  
  
  
   If @VSST_CNTR_PARA != 'S'  
   Begin  
     Select @VNNU_SECU = @VNNU_SECU + 1   
     Insert Into #TWTDORDE_FACT_Q01  
   (NU_SECU, CO_PART_OUTP, NU_GUIN_OUTP, NU_ORCO_OUTP, ST_CNTR_PART)  
    Values ( @VNNU_SECU, @CSCO_PART, @ISNU_DOCU_GUIN, @ISNU_DOCU_ORIG, 'N' )  
  End  
  
  
   If @VSTI_SITU = 'ACT' And @VSST_CNTR_PARA = 'S'  
   Begin  
  
      Select @VNIM_PART = Isnull(Sum(Isnull(CA_ORDE*IM_UNIT, 0)), 0),  
      @VNCA_PART = Isnull(Sum(Isnull(CA_ORDE, 0)), 0)  
      From   TDORDE_COMP   
      Where  NU_ORCO = @ISNU_DOCU_ORIG   
      And CO_EMPR = @ISCO_EMPR   
      And CO_PART = @CSCO_PART        
  
       Select @VNIM_REAL = (Select OFITESO.dbo.FN_TCFACT_CAMB_Q02(@VSCO_MONE_ORCO,   
              @VSCO_MONE_PART, @VDFE_EMIS_DOCU, @VNIM_PART, @VSTI_CAMB,   
       @VSTI_ORIG, 'S', @VSCO_MONE_NACI, @VSCO_MONE_EXTR, @INFA_TIPO_CAMB ))  
  
       --Se ejecuta el SP para determinar si existe presupuesto para la partida  
  
    SELECT @VNIM_SALD = 0  
  
       Exec OFIPRES..SP_TAPART_MENS_Q17 @ISCO_EMPR, @VNAA_CNTR, @VNMM_CNTR, 1, @CSCO_PART,   
       0, 0, @VNIM_REAL, @VNCA_PART, 'O', 'V', 'OFITESO', @OSVA_MENS Output, @VNIM_SALD Output    
  
       If Len(Isnull( Rtrim(Ltrim(@OSVA_MENS)), '')) != 0   
   Begin  
    Select @VNNU_SECU = @VNNU_SECU + 1   
     Insert Into #TWTDORDE_FACT_Q01  
    (NU_SECU, CO_PART_OUTP, NU_GUIN_OUTP, NU_ORCO_OUTP, ST_PRES_PART)  
    Values (@VNNU_SECU, @CSCO_PART, @ISNU_DOCU_GUIN, @ISNU_DOCU_ORIG,'N')  
    
  End   
   
   End  
  
 End  
  
  
    Fetch  CU_TDORDE_FACT_I01_1 Into @CSCO_PART  
  
    Select @VNNU_FETC_0001 = @@FETCH_STATUS  
  
 End --del cursor  
  
Close CU_TDORDE_FACT_I01_1  
Deallocate CU_TDORDE_FACT_I01_1  
  
  
End --del If  
  
--------------- CONTROL PRESUPUESTAL (FIN) --------------- */ 
--Commit Tran  
Return  
END  
/*-------------------------- Fin ------------------------------*/

GO
